package com.huitone.gddw.service.impl.monitoring.sms;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.monitoring.sms.IGateReportDao;
import com.huitone.gddw.service.dto.monitoring.sms.GateReportACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.GateReportDTO;
import com.huitone.gddw.service.dto.monitoring.sms.GateUpRepACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.GateUpRepDTO;
import com.huitone.gddw.service.monitoring.sms.IGateReportService;
import com.huitone.gddw.utils.DateTimeUtils;

/**
 * 短信通道监控 接口实现类
 * @author wuluodan
 *
 */
@Service
public class GateReportService implements IGateReportService {

	@Autowired
	private IGateReportDao gateReportDao;
	
	@Override
	public GateReportACDTO find(Map<String, Object> params) {
		boolean isNow = isIncluded(params);
		
		List<GateReportDTO> all = new ArrayList<GateReportDTO>();
		List<GateReportDTO> list = gateReportDao.find(params);
		GateReportACDTO gr = gateReportDao.findCount(params);
		if (isNow) {
//			String today = DateTimeUtils.dateToStr(new Date(), "yyyy-MM-dd");
//			Map<String, Object> curDate = new HashMap<String, Object>();
//			curDate.put("startDate", today);
//			curDate.put("endDate", today);
			List<GateReportDTO> grd = gateReportDao.selectSendSm(params);
			if (grd != null && grd.size() > 0) {
//				GateReportDTO g = gateReportDao.selectRecvSm(curDate);
//				gateReportDTO.setUpTotal(g == null ? 0 : g.getUpTotal());
				all.addAll(grd);
				
				GateReportACDTO gateReportACDTO = gateReportDao.countSendSm(params);
				if (gateReportACDTO != null) {
					gr.setTotalCount(gr.getTotalCount() + gateReportACDTO.getTotalCount());
					gr.setSendSucCount(gr.getSendSucCount() + gateReportACDTO.getSendSucCount());
					gr.setSendFailCount(gr.getSendFailCount() + gateReportACDTO.getSendFailCount());
					gr.setSendSucRateCount(gr.getTotalCount() == 0 
							? "0%" : String.format("%.2f", (float)gr.getSendSucCount() / gr.getTotalCount() * 100) + "%");
					gr.setUpNoneCount(gr.getUpNoneCount() + gateReportACDTO.getUpNoneCount());
				}
			}
		}
		all.addAll(list);
		gr.setList(all);
		return gr;
	}
	
	/**
	 * 判断是否查询当天数据
	 * @param params 日期
	 * @return boolean
	 */
	private boolean isIncluded(Map<String, Object> params) {
		boolean isNow = true;
		Date now = new Date();
		if (params.get("startDate") != null && !"".equals(params.get("startDate").toString())) {
			Date startDate = DateTimeUtils.strToDate(params.get("startDate").toString() + " 00:00:00");
			if (startDate.after(now)) {
				isNow = false;
			}
		}
		if (isNow && params.get("endDate") != null && !"".equals(params.get("endDate").toString())) {
			Date endDate = DateTimeUtils.strToDate(params.get("endDate").toString() + " 23:59:59");
			if (endDate.before(now)) {
				isNow = false;
			}
		}
		return isNow;
	}

	@Override
	public GateUpRepACDTO findUp(Map<String, Object> params) {
		List<GateUpRepDTO> all = new ArrayList<GateUpRepDTO>();
		List<GateUpRepDTO> list = gateReportDao.findUp(params);
		GateUpRepACDTO gr = gateReportDao.findCountUp(params);
		boolean isNow = isIncluded(params);
		if (isNow) {
//			String today = DateTimeUtils.dateToStr(new Date(), "yyyy-MM-dd");
//			Map<String, Object> curDate = new HashMap<String, Object>();
//			curDate.put("startDate", today);
//			curDate.put("endDate", today);
			List<GateUpRepDTO> grd = gateReportDao.selectRecvSm(params);
			if (grd != null && grd.size() > 0) {
				all.addAll(grd);
				
				GateUpRepACDTO gateUpRepACDTO = gateReportDao.countRecvSm(params);
				if (gateUpRepACDTO != null) {
					gr.setUpTotalCount(gr.getUpTotalCount() + gateUpRepACDTO.getUpTotalCount());
				}
			}
		}
		all.addAll(list);
		gr.setList(all);
		return gr;
	}

}
